package com.example.demo.controller;

import com.example.demo.entity.BuildingEntity;
import com.example.demo.entity.ClassroomEntity;
import com.example.demo.entity.PublicAreaEntity;
import com.example.demo.service.BuildingService;
import com.example.demo.service.PublicareaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * (Publicarea)表控制层
 *
 * @author makejava
 * @since 2021-10-12 15:29:39
 */
@Controller
public class PublicareaController {

    @Autowired
    private PublicareaService publicareaService;
    @Autowired
    private BuildingService buildingService;

    @RequestMapping("publicArealist")
    public String publicArealist(Model model){
        model.addAttribute("allPublicArea", publicareaService.queryPublicareaAll());
        return "publicArea-list";
    }

    @RequestMapping("toAddpublicArea")
    public String toAddpublicArea(Model model){
        model.addAttribute("allbuilding",buildingService.queryBuildingAll());
        return "publicArea-add";
    }

    @RequestMapping("addPublicArea")
    @ResponseBody
    public int AddpublicArea(PublicAreaEntity publicAreaEntity){
        int i = publicareaService.insertPublicarea(publicAreaEntity);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }
@RequestMapping("seleByidPublicArea")
    public String seleByidPublicArea(int paid,Model model){
        PublicAreaEntity publicAreaEntity = publicareaService.queryPublicareaById(paid);
        model.addAttribute("publicAreaEntity",publicAreaEntity);
        return "publicArea-update";
    }
    @RequestMapping("updatepublicArea")
    @ResponseBody
    public int updatepublicArea(PublicAreaEntity publicAreaEntity){
        int i = publicareaService.updatePublicarea(publicAreaEntity);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }
    @RequestMapping("deletePublicArea")
    @ResponseBody
    public int deletePublicArea(int paid){
        int i = publicareaService.deletePublicareaById(paid);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }

    //前往借公共区域
    @RequestMapping("toPublicAreaBorrow")
    public String toPublicAreaBorrow(HttpServletRequest request){
        List<BuildingEntity> buildingEntities = buildingService.queryBuildingAll();
        request.setAttribute("buildingEntities",buildingEntities);
//        List<PublicAreaEntity> publicAreaEntities = publicareaService.queryPublicareaAll();
//        request.setAttribute("publicAreaEntities",publicAreaEntities);
        return "publicArea-borrow";
    }

    //通过bid查公共区域
    @RequestMapping("showPublicareaByBid")
    @ResponseBody
    public List<PublicAreaEntity> showPublicareaByBid(int bid){
        return publicareaService.queryPublicareaByBid(bid);
    }


}